// pages/media/media.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: "",
    items: [
      "地理位置",
      "打电话",
      "摇一摇",
      "联系人"
    ],
    longitude: '',
    latitude: '',
    link: "https://image.etc.link/static/images/haibao/hb4.jpg"
  },
  drawImg() {
    wx.downloadFile({
      url: this.data.link,
      success: (res) => {
        console.log(res)
        wx.getImageInfo({
          src: res.tempFilePath,
          success: res => {
            const ctx = wx.createCanvasContext('twoCanvas')
            const unit = app.globalData.screenWidth / 750
            ctx.drawImage(res.path, 0, 0, 750 * unit, 1130 * unit);
            ctx.draw()
            wx.canvasToTempFilePath({
              canvasId: 'twoCanvas',
              width: unit * 750,
              height: unit * 1130,
              destWidth: unit * 2250,
              destHeight: unit * 3390,
              success: res => {
                let tempFilePath = res.tempFilePath;
                this.setData({
                  src: tempFilePath
                })
              },
              fail: err => {
                console.log(err)
              }
            })
          }
        })
      }
    })
  },
  drawCanvas() {
    var context = wx.createCanvasContext('firstCanvas')

    context.setStrokeStyle("#00ff00")
    context.setLineWidth(5)
    context.rect(0, 0, 200, 200)
    context.stroke()
    context.setStrokeStyle("#ff0000")
    context.setLineWidth(2)
    context.moveTo(160, 100)
    context.arc(100, 100, 60, 0, 2 * Math.PI, true)
    context.moveTo(140, 100)
    context.arc(100, 100, 40, 0, Math.PI, false)
    context.moveTo(85, 80)
    context.arc(80, 80, 5, 0, 2 * Math.PI, true)
    context.moveTo(125, 80)
    context.arc(120, 80, 5, 0, 2 * Math.PI, true)
    context.stroke()
    context.draw()
  },
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        })
      }
    })
  },
  openActions() {
    wx.showActionSheet({
      itemList: this.data.items,
      success: (res) => {
        console.log(res.tapIndex)
        const index = res.tapIndex
        if (index == 0) {
          wx.getLocation({
            type: 'wgs84',
            success: (res) => {
              const latitude = res.latitude
              const longitude = res.longitude
              const speed = res.speed
              const accuracy = res.accuracy
              this.setData({
                latitude,
                longitude
              })
              // 打开微信地图 
              wx.openLocation({
                latitude,
                longitude,
                scale: 18,
                name: '千锋教育',
                address: "金融港B18栋3楼"
              })
            }
          })
        } else if (index == 1) {
          wx.makePhoneCall({
            phoneNumber: '15717824519'
          })
        } else if (index == 2) {
          wx.vibrateLong()
        } else if (index == 3) {
          wx.chooseContact()
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  todoScan() {
    // 允许从相机和相册扫码
    wx.scanCode({
      success(res) {
        console.log(res)
      }
    })
  },
  todogetPhoto() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // tempFilePath可以作为 img 标签的 src 属性显示图片
        const tempFilePaths = res.tempFilePaths;
        this.setData({
          src: res.tempFilePaths
        })
      }
    })

    // wx.chooseMedia({
    //   count: 1,
    //   mediaType: ['image'],
    //   sourceType: ['album', 'camera'],
    //   maxDuration: 30,
    //   camera: 'front',
    //   success:(res)=> {
    //     console.log(res.tempFiles.tempFilePath)
    //     console.log(res.tempFiles.size)
    //     this.setData({
    //       src:res.tempFiles.tempFilePath
    //     })
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.drawCanvas()
    this.drawImg()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})